| Conditions | 7 |
| Total Lines | 79 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import * as fs from "fs"; |
||
| 102 | export function serve(port: number = 3000) { |
||
| 103 | const httpServer = http.createServer(function (req, res) { |
||
| 104 | var url = req.url; |
||
| 105 | //console.log(url); |
||
| 106 | var routerStatic = path.join(__dirname, `/components/router/${url}.js`); |
||
| 107 | //console.log(routerStatic, fs.existsSync(routerStatic)); |
||
| 108 | |||
| 109 | if (url === "/") { |
||
| 110 | res.write(template("Homepage", "index", [req, res])); //write a response |
||
| 111 | } else if (url === "/fetch") { |
||
| 112 | res.write( |
||
| 113 | template( |
||
| 114 | function () { |
||
| 115 | var installed = "./tmp/npm/local.json"; |
||
| 116 | var result = {}; |
||
| 117 | try { |
||
| 118 | if (fs.existsSync(installed)) { |
||
| 119 | result = JSON.parse(fs.readFileSync(installed).toString()); |
||
| 120 | } else { |
||
| 121 | list_package(); |
||
| 122 | result = { |
||
| 123 | error: "package still not fetched", |
||
| 124 | local: {}, |
||
| 125 | global: {}, |
||
| 126 | }; |
||
| 127 | } |
||
| 128 | } catch (error) { |
||
| 129 | if (error) { |
||
| 130 | result = {}; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | return JSON.stringify(result, null, 2); |
||
| 135 | }, |
||
| 136 | [req, res] |
||
| 137 | ) |
||
| 138 | ); //write a response |
||
| 139 | } else if (fs.existsSync(routerStatic)) { |
||
| 140 | var router = require(path.join(__dirname, "/components/router", url)) |
||
| 141 | .router; |
||
| 142 | res.write(template(router(), [req, res])); |
||
| 143 | } else { |
||
| 144 | res.writeHead(302, { |
||
| 145 | Location: `${config.app.protocol}://${config.app.domain}/${ |
||
| 146 | url_core.parse(url).pathname |
||
| 147 | }`, |
||
| 148 | //add other headers here... |
||
| 149 | }); |
||
| 150 | } |
||
| 151 | |||
| 152 | res.end(); //end the response |
||
| 153 | }); |
||
| 154 | |||
| 155 | const webSocket = io(httpServer); |
||
| 156 | webSocket.on("connect", (socket) => { |
||
| 157 | console.log("websocket connected", socket.id); |
||
| 158 | }); |
||
| 159 | var numClients = 0; |
||
| 160 | webSocket.on("connection", function (socket) { |
||
| 161 | numClients++; |
||
| 162 | socket.emit("stats", { numClients: numClients }); |
||
| 163 | |||
| 164 | console.log("Connected clients:", numClients); |
||
| 165 | |||
| 166 | socket.on("disconnect", function () { |
||
| 167 | numClients--; |
||
| 168 | socket.emit("stats", { numClients: numClients }); |
||
| 169 | |||
| 170 | console.log("Connected clients:", numClients); |
||
| 171 | }); |
||
| 172 | |||
| 173 | socket.emit("announcements", { message: "A new user has joined!" }); |
||
| 174 | socket.on("fetch", function (data) { |
||
| 175 | list_package(); |
||
| 176 | }); |
||
| 177 | }); |
||
| 178 | |||
| 179 | httpServer.listen(port, function () { |
||
| 180 | console.log("server start at port " + port); |
||
| 181 | }); |
||
| 183 |